﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ongoal.Quotation.Common
{
    /// <summary>
    /// 文件读写帮助类
    /// </summary>
    public class FileHelper
    {
        private static Encoding _defaultEncoding = Encoding.UTF8;

        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="content">文件内容</param>
        public static void WriteString(string filePath, string content)
        {
            var file = new FileInfo(filePath);
            if (!Directory.Exists(file.DirectoryName))
                Directory.CreateDirectory(file.DirectoryName!);

            File.WriteAllText(filePath, content, _defaultEncoding);
        }

        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="content">文件内容</param>
        /// <param name="encoding">文件编码</param>
        public static void WriteString(string filePath, string content, Encoding encoding)
        {
            var file = new FileInfo(filePath);
            if (!Directory.Exists(file.DirectoryName))
                Directory.CreateDirectory(file.DirectoryName!);

            File.WriteAllText(filePath, content, encoding);
        }

        /// <summary>
        /// 读文件
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="encoding">文件编码</param>
        /// <returns>文件内容</returns>
        public static string ReadString(string filePath, Encoding encoding)
        {
            var file = new FileInfo(filePath);
            if (!File.Exists(filePath))
                return null!;

            return File.ReadAllText(filePath, encoding);
        }

        /// <summary>
        /// 读文件
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>文件内容</returns>
        public static string ReadString(string filePath)
        {
            var file = new FileInfo(filePath);
            if (!File.Exists(filePath))
                return null!;

            return File.ReadAllText(filePath, _defaultEncoding);
        }
    }
}
